/**
 * 组件事件定义
 * 标准化组件间通信的事件类型和数据格式
 */

/**
 * 标准事件类型常量
 */
export const COMPONENT_EVENTS = {
    // 装备相关事件
    EQUIPMENT: {
        EQUIPPED: 'equipment.equipped',           // 装备穿戴
        UNEQUIPPED: 'equipment.unequipped',       // 装备卸载
        REPLACED: 'equipment.replaced',           // 装备替换
        DESTROYED: 'equipment.destroyed',         // 装备销毁
        STATS_CHANGED: 'equipment.stats_changed'  // 装备属性变化
    },

    // 背包相关事件
    BAG: {
        ITEM_ADDED: 'bag.item_added',             // 物品添加
        ITEM_REMOVED: 'bag.item_removed',         // 物品移除
        ITEM_USED: 'bag.item_used',               // 物品使用
        GOLD_CHANGED: 'bag.gold_changed',         // 金币变化
        CAPACITY_CHANGED: 'bag.capacity_changed'  // 容量变化
    },

    // RPG相关事件
    RPG: {
        LEVEL_UP: 'rpg.level_up',                 // 升级
        EXP_GAINED: 'rpg.exp_gained',             // 获得经验
        HP_CHANGED: 'rpg.hp_changed',             // 生命值变化
        POWER_CHANGED: 'rpg.power_changed',       // 战力变化
        ATTRIBUTE_CHANGED: 'rpg.attribute_changed', // 属性变化
        SKILL_LEARNED: 'rpg.skill_learned',       // 学会技能
        SKILL_UPGRADED: 'rpg.skill_upgraded',     // 技能升级
        BATTLE_STARTED: 'rpg.battle_started',     // 战斗开始
        BATTLE_ENDED: 'rpg.battle_ended'          // 战斗结束
    },

    // 血统相关事件
    TALENT: {
        ADDED: 'talent.added',                    // 血统获得
        UPGRADED: 'talent.upgraded',              // 血统升级
        EXP_GAINED: 'talent.exp_gained',          // 血统经验获得
        LEVEL_UP: 'talent.level_up'               // 血统升级
    },

    // 随从相关事件
    FOLLOW: {
        ADDED: 'follow.added',                    // 随从添加
        REMOVED: 'follow.removed',                // 随从移除
        LEVEL_UP: 'follow.level_up',              // 随从升级
        EXP_GAINED: 'follow.exp_gained'           // 随从经验获得
    },

    // 任务相关事件
    TASK: {
        ACCEPTED: 'task.accepted',                // 任务接受
        COMPLETED: 'task.completed',              // 任务完成
        PROGRESS_UPDATED: 'task.progress_updated', // 任务进度更新
        REWARD_CLAIMED: 'task.reward_claimed'     // 奖励领取
    },

    // 签到相关事件
    SIGN: {
        SIGNED: 'sign.signed',                    // 签到
        STREAK_UPDATED: 'sign.streak_updated',    // 连续签到更新
        REWARD_RECEIVED: 'sign.reward_received'   // 签到奖励获得
    },

    // 系统事件
    SYSTEM: {
        COMPONENT_LOADED: 'system.component_loaded',     // 组件加载
        COMPONENT_DESTROYED: 'system.component_destroyed', // 组件销毁
        DATA_SAVED: 'system.data_saved',               // 数据保存
        ERROR_OCCURRED: 'system.error_occurred'        // 错误发生
    }
} as const;

/**
 * 事件数据接口定义
 */

// 装备事件数据
export interface EquipmentEventData {
    playerId: string;
    equipment: any;
    slot?: number;
    oldEquipment?: any;
    stats?: any;
}

// 背包事件数据
export interface BagEventData {
    playerId: string;
    item?: any;
    oldAmount?: number;
    newAmount?: number;
    goldAmount?: number;
    reason?: string;
}

// RPG事件数据
export interface RPGEventData {
    playerId: string;
    oldLevel?: number;
    newLevel?: number;
    expGained?: number;
    oldHP?: number;
    newHP?: number;
    oldPower?: number;
    newPower?: number;
    attribute?: {
        name: string;
        oldValue: number;
        newValue: number;
    };
    skill?: any;
    battleResult?: any;
}

// 血统事件数据
export interface TalentEventData {
    playerId: string;
    talent: any;
    oldLevel?: number;
    newLevel?: number;
    expGained?: number;
}

// 随从事件数据
export interface FollowEventData {
    playerId: string;
    follow: any;
    oldLevel?: number;
    newLevel?: number;
    expGained?: number;
}

// 任务事件数据
export interface TaskEventData {
    playerId: string;
    task: any;
    progress?: number;
    completed?: boolean;
    rewards?: any[];
}

// 签到事件数据
export interface SignEventData {
    playerId: string;
    signData: any;
    rewards?: any[];
    consecutiveDays?: number;
}

// 系统事件数据
export interface SystemEventData {
    component?: string;
    playerId?: string;
    error?: Error;
    data?: any;
    message?: string;
}

/**
 * 事件数据类型映射
 */
export type ComponentEventDataMap = {
    // 装备事件
    [COMPONENT_EVENTS.EQUIPMENT.EQUIPPED]: EquipmentEventData;
    [COMPONENT_EVENTS.EQUIPMENT.UNEQUIPPED]: EquipmentEventData;
    [COMPONENT_EVENTS.EQUIPMENT.REPLACED]: EquipmentEventData;
    [COMPONENT_EVENTS.EQUIPMENT.DESTROYED]: EquipmentEventData;
    [COMPONENT_EVENTS.EQUIPMENT.STATS_CHANGED]: EquipmentEventData;

    // 背包事件
    [COMPONENT_EVENTS.BAG.ITEM_ADDED]: BagEventData;
    [COMPONENT_EVENTS.BAG.ITEM_REMOVED]: BagEventData;
    [COMPONENT_EVENTS.BAG.ITEM_USED]: BagEventData;
    [COMPONENT_EVENTS.BAG.GOLD_CHANGED]: BagEventData;
    [COMPONENT_EVENTS.BAG.CAPACITY_CHANGED]: BagEventData;

    // RPG事件
    [COMPONENT_EVENTS.RPG.LEVEL_UP]: RPGEventData;
    [COMPONENT_EVENTS.RPG.EXP_GAINED]: RPGEventData;
    [COMPONENT_EVENTS.RPG.HP_CHANGED]: RPGEventData;
    [COMPONENT_EVENTS.RPG.POWER_CHANGED]: RPGEventData;
    [COMPONENT_EVENTS.RPG.ATTRIBUTE_CHANGED]: RPGEventData;
    [COMPONENT_EVENTS.RPG.SKILL_LEARNED]: RPGEventData;
    [COMPONENT_EVENTS.RPG.SKILL_UPGRADED]: RPGEventData;
    [COMPONENT_EVENTS.RPG.BATTLE_STARTED]: RPGEventData;
    [COMPONENT_EVENTS.RPG.BATTLE_ENDED]: RPGEventData;

    // 血统事件
    [COMPONENT_EVENTS.TALENT.ADDED]: TalentEventData;
    [COMPONENT_EVENTS.TALENT.UPGRADED]: TalentEventData;
    [COMPONENT_EVENTS.TALENT.EXP_GAINED]: TalentEventData;
    [COMPONENT_EVENTS.TALENT.LEVEL_UP]: TalentEventData;

    // 随从事件
    [COMPONENT_EVENTS.FOLLOW.ADDED]: FollowEventData;
    [COMPONENT_EVENTS.FOLLOW.REMOVED]: FollowEventData;
    [COMPONENT_EVENTS.FOLLOW.LEVEL_UP]: FollowEventData;
    [COMPONENT_EVENTS.FOLLOW.EXP_GAINED]: FollowEventData;

    // 任务事件
    [COMPONENT_EVENTS.TASK.ACCEPTED]: TaskEventData;
    [COMPONENT_EVENTS.TASK.COMPLETED]: TaskEventData;
    [COMPONENT_EVENTS.TASK.PROGRESS_UPDATED]: TaskEventData;
    [COMPONENT_EVENTS.TASK.REWARD_CLAIMED]: TaskEventData;

    // 签到事件
    [COMPONENT_EVENTS.SIGN.SIGNED]: SignEventData;
    [COMPONENT_EVENTS.SIGN.STREAK_UPDATED]: SignEventData;
    [COMPONENT_EVENTS.SIGN.REWARD_RECEIVED]: SignEventData;

    // 系统事件
    [COMPONENT_EVENTS.SYSTEM.COMPONENT_LOADED]: SystemEventData;
    [COMPONENT_EVENTS.SYSTEM.COMPONENT_DESTROYED]: SystemEventData;
    [COMPONENT_EVENTS.SYSTEM.DATA_SAVED]: SystemEventData;
    [COMPONENT_EVENTS.SYSTEM.ERROR_OCCURRED]: SystemEventData;
};

/**
 * 事件工厂类 - 便捷创建标准事件
 */
export class ComponentEventFactory {
    /**
     * 创建装备穿戴事件
     */
    static createEquipmentEquipped(playerId: string, equipment: any, slot?: number): EquipmentEventData {
        return { playerId, equipment, slot };
    }

    /**
     * 创建装备卸载事件
     */
    static createEquipmentUnequipped(playerId: string, equipment: any, slot?: number): EquipmentEventData {
        return { playerId, equipment, slot };
    }

    /**
     * 创建装备替换事件
     */
    static createEquipmentReplaced(playerId: string, equipment: any, oldEquipment: any, slot?: number): EquipmentEventData {
        return { playerId, equipment, oldEquipment, slot };
    }

    /**
     * 创建物品添加事件
     */
    static createItemAdded(playerId: string, item: any, reason?: string): BagEventData {
        return { playerId, item, reason };
    }

    /**
     * 创建物品移除事件
     */
    static createItemRemoved(playerId: string, item: any, reason?: string): BagEventData {
        return { playerId, item, reason };
    }

    /**
     * 创建金币变化事件
     */
    static createGoldChanged(playerId: string, oldAmount: number, newAmount: number, reason?: string): BagEventData {
        return { playerId, oldAmount, newAmount, goldAmount: newAmount - oldAmount, reason };
    }

    /**
     * 创建升级事件
     */
    static createLevelUp(playerId: string, oldLevel: number, newLevel: number): RPGEventData {
        return { playerId, oldLevel, newLevel };
    }

    /**
     * 创建经验获得事件
     */
    static createExpGained(playerId: string, expGained: number): RPGEventData {
        return { playerId, expGained };
    }

    /**
     * 创建战力变化事件
     */
    static createPowerChanged(playerId: string, oldPower: number, newPower: number): RPGEventData {
        return { playerId, oldPower, newPower };
    }

    /**
     * 创建属性变化事件
     */
    static createAttributeChanged(playerId: string, attributeName: string, oldValue: number, newValue: number): RPGEventData {
        return { 
            playerId, 
            attribute: { 
                name: attributeName, 
                oldValue, 
                newValue 
            } 
        };
    }

    /**
     * 创建技能学会事件
     */
    static createSkillLearned(playerId: string, skill: any): RPGEventData {
        return { playerId, skill };
    }

    /**
     * 创建血统获得事件
     */
    static createTalentAdded(playerId: string, talent: any): TalentEventData {
        return { playerId, talent };
    }

    /**
     * 创建血统升级事件
     */
    static createTalentUpgraded(playerId: string, talent: any, oldLevel: number, newLevel: number): TalentEventData {
        return { playerId, talent, oldLevel, newLevel };
    }

    /**
     * 创建随从添加事件
     */
    static createFollowAdded(playerId: string, follow: any): FollowEventData {
        return { playerId, follow };
    }

    /**
     * 创建随从移除事件
     */
    static createFollowRemoved(playerId: string, follow: any): FollowEventData {
        return { playerId, follow };
    }

    /**
     * 创建任务完成事件
     */
    static createTaskCompleted(playerId: string, task: any, rewards?: any[]): TaskEventData {
        return { playerId, task, completed: true, rewards };
    }

    /**
     * 创建签到事件
     */
    static createSigned(playerId: string, signData: any, rewards?: any[], consecutiveDays?: number): SignEventData {
        return { playerId, signData, rewards, consecutiveDays };
    }

    /**
     * 创建系统错误事件
     */
    static createSystemError(component: string, error: Error, playerId?: string): SystemEventData {
        return { component, error, playerId };
    }
}
